﻿//----------------------------------------------------------------
// <copyright file="TeamProjectSection.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace MptCodeLibrary.Configuration
{
    public class TeamProjectConfiguration : ConfigurationElement
    {
        private const string TeamProjectName = "teamprojectname";

        /// <summary>
        /// Gets or sets the TFS server name
        /// </summary>
        [ConfigurationProperty(TeamProjectName, IsKey = true, IsRequired = true)]
        public string ProjectName
        {
            get { return (string)base[TeamProjectName]; }
            set { base[TeamProjectName] = value; }
        }
    }

    public class TeamProjectSection : ConfigurationSection, ITeamProjectConfigurationList
    {
        const string TeamProjectsProperty = "teamprojects";

        /// <summary>
        /// Gets the list of team servers from the config file section.
        /// </summary>
        /// <value>The team server list.</value>
        [ConfigurationCollection(typeof(TeamProjectConfiguration), AddItemName = "teamproject", CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
        [ConfigurationProperty(TeamProjectsProperty)]
        public virtual TeamProjectConfigurationCollection TeamProjects
        {
            get { return (TeamProjectConfigurationCollection)base[TeamProjectsProperty]; }
        }

        /// <summary>
        /// Gets an enumerator for the list of the team servers from the config file section
        /// </summary>
        /// <returns>An enumerator</returns>
        public IEnumerable<TeamProjectConfiguration> GetTeamProjects()
        {
            return TeamProjects;
        }
    }

    interface ITeamProjectConfigurationList
    {
        /// <summary>
        /// Gets an enumerator for the list of the team servers from the config file section
        /// </summary>
        /// <returns>An enumerator</returns>
        IEnumerable<TeamProjectConfiguration> GetTeamProjects();
    }

    /// <summary>
    /// Collection of Web.config rows, in TeamProjectSubSection. 
    /// </summary>
    public class TeamProjectConfigurationCollection : ConfigurationElementCollection, IEnumerable<TeamProjectConfiguration>
    {
        ///<summary>
        ///Returns an enumerator that iterates through the collection of TeamProjectConfiguration.
        ///</summary>
        ///<returns>
        ///A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        ///</returns>
        ///<filterpriority>1</filterpriority>
        public virtual new IEnumerator<TeamProjectConfiguration> GetEnumerator()
        {
            IEnumerator enumerator = base.GetEnumerator();
            while (enumerator.MoveNext())
            {
                yield return (TeamProjectConfiguration)enumerator.Current;
            }
        }

        ///<summary>
        ///Creates a new <see cref="T:System.Configuration.ConfigurationElement"></see>.
        ///</summary>
        ///<returns>
        ///A new <see cref="T:System.Configuration.ConfigurationElement"></see> of derived type TeamProjectConfiguration.
        ///</returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new TeamProjectConfiguration();
        }

        ///<summary>
        ///Gets the element key, ServerName, for a TeamProjectSubSection row configuration element.
        ///</summary>
        ///<returns>
        ///An <see cref="T:System.Object"></see> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"></see>.
        ///</returns>
        ///<param name="element">The <see cref="T:System.Configuration.ConfigurationElement"></see> to return the key for. </param>
        protected override object GetElementKey(ConfigurationElement element)
        {
            TeamProjectConfiguration teamProjectConfiguration = (TeamProjectConfiguration)element;

            return teamProjectConfiguration.ProjectName;
        }
    }
}
